<?php
namespace app\api\model\foodcash;

use app\common\model\Order as OrderModel;
use app\api\model\Record as RecordModel;
use hema\Helper;
use think\facade\Db;

/**
 * 订单模型
 */
class Order extends OrderModel
{
    /**
     * 隐藏字段
     */
    protected $hidden = [
    ];	

    /**
     * 订单确认-购物车结算
     */
    public function getCart($user_id,$shop_id,$order_mode)
    {
        $model = new Cart($user_id,$shop_id);
        return $model->getList($order_mode);
    }
    
    /**
     * 新增订单
     */
    public function add($order)
    {
        // 开启事务
        Db::startTrans();
        try {
            // 记录订单信息
            $this->save($order);
            // 保存订单商品信息
            $this->goods()->saveAll($order['goodsList']);
			//如果是外卖,记录收货地址
			if($order['order_mode'] == 20){
			    if($order['address']['is_vip'] == 1){
    		        $user = User::get($order['user_id'],['addressDefault']);
    		        $address = [
    					'user_id' => $order['user_id'],
    					'applet_id' => $order['applet_id'],
    					'name' => $user['addressDefault']['name'],
    					'phone' => $user['addressDefault']['phone'],
    					'location' => $user['addressDefault']['location'],
    					'province' => $user['addressDefault']['province'],
    					'city' => $user['addressDefault']['city'],
    					'district' => $user['addressDefault']['district'],
    					'detail' => $user['addressDefault']['detail'],
    				];
    		    }else{
    		       $address = [
    					'user_id' => $order['user_id'],
    					'applet_id' => $order['applet_id'],
    					'phone' => $order['address']['phone'],
    					'detail' => $order['address']['detail'],
    				]; 
    		    }
				$this->address()->save($address);
			}
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
        }
        return false;
    }
 
























    
	
	/**
     * 开始配送
     */
    public function receipt_star()
    {
        if ($this['pay_status']['value'] == 10
            || $this['delivery_status']['value'] == 10) {
            $this->error = '该订单不合法';
            return false;
        }
		post_tpl('delivery',$this->order_id);
        return $this->save([
            'receipt_status' => 20,
            'receipt_time' => time(),
        ]);
    }
	
	/**
     * 配送完成
     */
    public function receipt_end()
    {
        if ($this['pay_status']['value'] == 10
            || $this['delivery_status']['value'] == 10) {
            $this->error = '该订单不合法';
            return false;
        }
        return $this->save([
            'receipt_status' => 30,
            'receipt_time' => time(),
        ]);
    }

    /**
     * 取消订单
     */
    public function cancel()
    {
        if ($this['pay_status']['value'] == 20) {
            $this->error = '已付款订单不可取消';
            return false;
        }
        // 回退商品库存
        //$this->backGoodsStock($this['goods']);
        return $this->save(['order_status' => 20]);
    }

    /**
     * 回退商品库存
     
    private function backGoodsStock(&$goodsList)
    {
        $goodsSpecSave = [];
        foreach ($goodsList as $goods) {
            // 下单减库存
            if ($goods['deduct_stock_type'] == 10) {
                $goodsSpecSave[] = [
                    'goods_spec_id' => $goods['goods_spec_id'],
                    'stock_num' => ['inc', $goods['total_num']]
                ];
            }
        }
        // 更新商品规格库存
        return !empty($goodsSpecSave) && (new GoodsSpec)->isUpdate()->saveAll($goodsSpecSave);
    }
*/
	
}
